package com.bl.weilan.service.biz.search.impl;

import com.bl.weilan.dao.model.CompanyEntity;
import com.bl.weilan.dao.model.InstitutionEntity;
import com.bl.weilan.dao.model.NewsEntity;
import com.bl.weilan.dao.model.ReportEntity;
import com.bl.weilan.dao.repository.CompanyRepository;
import com.bl.weilan.dao.repository.InstitutionRepository;
import com.bl.weilan.dao.repository.NewsRepository;
import com.bl.weilan.dao.repository.ReportRepository;
import com.bl.weilan.service.biz.search.SearchService;
import com.bl.weilan.service.vo.company.CompanyListVo;
import com.bl.weilan.service.vo.company.CompanyOnlyListVo;
import com.bl.weilan.service.vo.institution.InstitutionListVo;
import com.bl.weilan.service.vo.news.NewsListVo;
import com.bl.weilan.service.vo.report.ReportListVo;
import com.bl.weilan.service.vo.search.SearchListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Log4j2
public class SearchServiceImpl implements SearchService {
    @Autowired
    ReportRepository reportRepository;

    @Autowired
    CompanyRepository companyRepository;

    @Autowired
    InstitutionRepository institutionRepository;

    @Autowired
    NewsRepository newsRepository;


    @Override
    public InstitutionListVo searchInstitutionEntityByName(String name, Pageable pageable){

        InstitutionListVo institutionListVo = new InstitutionListVo();
        Page<InstitutionEntity> institutionEntities;

        institutionEntities = institutionRepository.findByNameLike("%"+name+ "%", pageable);
        institutionListVo.setTotalCount(institutionEntities.getTotalElements());
        institutionListVo.setList(institutionEntities.getContent());

        return institutionListVo;

    }

    @Override
    public ReportListVo searchReportEntityByName(String name, Pageable pageable){

        ReportListVo reportListVo = new ReportListVo();
        Page<ReportEntity> reportEntities;

        reportEntities = reportRepository.findByTitleLike("%"+name+ "%", pageable);
        reportListVo.setTotalCount(reportEntities.getTotalElements());
        reportListVo.setList(reportEntities.getContent());

        return reportListVo;

    }

    @Override
    public CompanyOnlyListVo searchCompanyEntityEntityByName(String name, Pageable pageable){

        CompanyOnlyListVo companyOnlyListVo = new CompanyOnlyListVo();
        Page<CompanyEntity> companyEntities;

        companyEntities = companyRepository.findByFullNameLike("%"+name+ "%", pageable);
        companyOnlyListVo.setTotalCount(companyEntities.getTotalElements());
        companyOnlyListVo.setList(companyEntities.getContent());

        return companyOnlyListVo;

    }

    @Override
    public NewsListVo searchNewsEntityByName(String name, Pageable pageable){

        NewsListVo newsListVo = new NewsListVo();
        Page<NewsEntity> newsEntities;

        newsEntities = newsRepository.findByTitleLike("%"+name+ "%", pageable);
        newsListVo.setTotalCount(newsEntities.getTotalElements());
        newsListVo.setList(newsEntities.getContent());

        return newsListVo;

    }
}
